package com.xwm93up.cancelorder.job;

import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

/**
 * @Description: 自定义MyJob实现轮询数据库
 * @Author: xwm93up
 * @Since: 2021/11/1 11:20
 */
public class MyJob implements Job {
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("要去数据库扫描啦....");
    }

    public static void main(String[] args) throws SchedulerException {
        // 创建任务
        JobDetail jobDetail = JobBuilder.newJob(MyJob.class).withIdentity("job1", "group1").build();
        // 创建触发器, 每隔3秒执行一次
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group3").withSchedule(
                SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(3).repeatForever()
        ).build();
        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        // 将任务及触发器放入调度器
        scheduler.scheduleJob(jobDetail, trigger);
        // 调度器开始调度任务
        scheduler.start();
    }
}
